# @File: router.py
# -*- coding: utf-8 -*-
# __author:"super"
# @time:2025/7/25 14:19

import re

from get_define.base_model import EndpointConfig, HTTPMethod

"""
API 路由装饰器模块，用于为 API 类配置路由信息
"""


class APIRouter:

    def route(self, path: str, method: HTTPMethod, **kwargs):
        def decorator(cls):
            route_params = re.findall(r'{(\w+)}', path)
            endpoint_config = EndpointConfig(
                route=path,
                method=method,
                route_params=route_params,
            )

            setattr(cls, '_endpoint_config', endpoint_config)
            return cls

        return decorator

    def get(self, path: str, **kwargs):
        return self.route(path, HTTPMethod.GET, **kwargs)

    def post(self, path: str, **kwargs):
        return self.route(path, HTTPMethod.POST, **kwargs)

    def put(self, path: str, **kwargs):
        return self.route(path, HTTPMethod.PUT, **kwargs)

    def delete(self, path: str, **kwargs):
        return self.route(path, HTTPMethod.DELETE, **kwargs)

    def patch(self, path: str, **kwargs):
        return self.route(path, HTTPMethod.PATCH, **kwargs)


router = APIRouter()
